package o2o.web.actions.pay;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysConfigPayMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.constant.PAY_CONFIG;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysConfigPay;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysConfigPayVO;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import o2o.system.Common;

import java.sql.SQLException;

@Controller
public class PayConfigAction {

    //获取支付配置信息--手机端
    @RequestMapping(method = RequestMethod.POST, value = "/pay/config/get")
    @ResponseBody
    public String get_pay_config(String sysAppKey, String appFlag) {
        SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByKey(sysAppKey, appFlag);
        if (null == sysConfigPay) {
            return AjaxResult.errorResult("当前记录不存在");
        }
        SysConfigPayVO vo = new SysConfigPayVO();
        try {
            ObjectUtils.copyPropertiesExclude(sysConfigPay, vo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取失败，请稍后再试");
        }

        return AjaxResult.successResult(vo);
    }


    //修改支付配置信息
    @RequestMapping(method = RequestMethod.POST, value = "/pay/config/set")
    @ResponseBody
    public String set_pay(String token, SysConfigPayVO info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有修改权限");
        }

        if (null == info) {
            return AjaxResult.errorResult("输入不完整");
        }

        if (ValidateUtil.isNotID(info.getId())) {
            return AjaxResult.errorResult("请先选择要修改的");
        }

        SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getById(info.getId());
        if (null == sysConfigPay) {
            return AjaxResult.errorResult("当前记录不存在");
        }

        if (null == info.getIsAliAble()) {
            info.setIsAliAble(0);
        }
        if (null == info.getIsWxAble()) {
            info.setIsWxAble(0);
        }

        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SYS_CONFIG_PAY).append(" set isAliAble = ").append(info.getIsAliAble())
                .append(",isWxAble = ").append(info.getIsWxAble()).append(",showNote = '").append(info.getShowNote())
                .append("',updateUser = '").append(user.getUsername()).append("' where id =").append(info.getId()).append(" and delFlag =0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }
        //更新缓存
        SysConfigPayMemory.getInstance().modifyById(info.getId());

        //修改成功后重新加载一下
        PAY_CONFIG.init();

        return AjaxResult.successResult();
    }

}
